
  
  <script setup>
  import { ref, reactive } from 'vue';
  import { ElMessage } from 'element-plus';
  
  import { updateTeacherPasswordService } from '@/api/user.js'
  // 表单数据
  const form = reactive({
    currentPassword: '',
    newPassword: '',
    confirmPassword: '',
  });
  
  // 错误和成功消息
  const errorMessage = ref('');
  const successMessage = ref('');
  
  // 表单验证规则
  const rules = {
    currentPassword: [
      { required: true, message: '请输入原密码', trigger: 'blur' },
    ],
    newPassword: [
      { required: true, message: '请输入新密码', trigger: 'blur' },
      { min: 6, message: '新密码长度不能少于6位', trigger: 'blur' },
    ],
    confirmPassword: [
      { required: true, message: '请确认新密码', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value !== form.newPassword) {
            callback(new Error('两次输入的密码不一致'));
          } else {
            callback();
          }
        },
        trigger: 'blur',
      },
    ],
  };
  
  // 表单引用
  const passwordForm = ref(null);
  
  // 提交表单
  const handleSubmit = () => {
    passwordForm.value.validate((valid) => {
      if (valid) {
        updateTeacherPasswordService(form)
        .then(() => {
      ElMessage.success('修改密码成功');
    })
    .catch(() => {
      ElMessage.error('修改密码失败');
    });
        // 清空表单
          form.currentPassword = '';
          form.newPassword = '';
          form.confirmPassword = '';
      } else {
        errorMessage.value = '请检查表单内容';
        return false;
      }
    });
  };
  </script>
  
  <template>
    <el-card class="password-change-form">
      <h2>修改密码</h2>
      <el-form :model="form" :rules="rules" ref="passwordForm" @submit.prevent="handleSubmit">
        <!-- 原密码 -->
        <el-form-item label="原密码" prop="currentPassword">
          <el-input
            type="password"
            v-model="form.currentPassword"
            placeholder="请输入原密码"
            show-password
          ></el-input>
        </el-form-item>
  
        <!-- 新密码 -->
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            type="password"
            v-model="form.newPassword"
            placeholder="请输入新密码"
            show-password
          ></el-input>
        </el-form-item>
  
        <!-- 确认新密码 -->
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input
            type="password"
            v-model="form.confirmPassword"
            placeholder="请再次输入新密码"
            show-password
          ></el-input>
        </el-form-item>
  
        <!-- 按钮居中 -->
        <el-form-item class="button-container">
          <el-button type="primary" native-type="submit">修改密码</el-button>
        </el-form-item>
      </el-form>
  
      <!-- 错误消息 -->
      <el-alert
        v-if="errorMessage"
        :title="errorMessage"
        type="error"
        show-icon
        :closable="false"
      ></el-alert>
  
      <!-- 成功消息 -->
      <el-alert
        v-if="successMessage"
        :title="successMessage"
        type="success"
        show-icon
        :closable="false"
      ></el-alert>
    </el-card>
  </template>

  <style scoped>
  .password-change-form {
    max-width: 400px;
    margin: 0 auto;
    padding: 20px;
  }
  
  h2 {
    text-align: center;
    margin-bottom: 20px;
  }
  
  .el-form-item {
    margin-bottom: 20px;
  }
  
  .button-container {
    text-align: center; /* 按钮居中 */
    margin-top: 20px; /* 与 <h2> 保持垂直对齐 */
  }
  
  .el-alert {
    margin-top: 20px;
  }
  </style>